package com.brkj.communityStudy;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import com.brkj.communityStudy.model.DS_QAStudy;
import com.brkj.communityStudy.model.DS_QAStudy;
import com.brkj.httpInterface.BaseAjaxParams;
import com.brkj.httpInterface.HttpInterface;
import com.brkj.httpInterface.NomalResult;
import com.brkj.main.R;
import com.brkj.util.ImgShow;
import com.brkj.util.MyAjaxCallBack;
import com.brkj.util.MyApplication;
import com.brkj.util.view.BaseCoreActivity;
import net.tsz.afinal.FinalHttp;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by colin.ding on 2014/5/16.
 */

public class QAAdapter extends BaseAdapter {
    private ListItemView listItemView;
    protected LayoutInflater listContainer;
    private Context context;
    private List<DS_QAStudy> QAList;

    private Map<Integer, Integer> haveAddMap = new HashMap<Integer, Integer>();

    public QAAdapter(Context context, List<DS_QAStudy> QAList){
        this.context = context;
        this.QAList = QAList;

        listContainer = LayoutInflater.from(context);

    }

    private static class ListItemView{
        public TextView question;
        public TextView Time;
        public TextView answerNum;
        public View answerView;
        public ImageView answerHead;
        public TextView answerName;
        public TextView answer;
    };

    @Override
    public int getCount() {
        // TODO Auto-generated method stub
        return QAList.size();
    }

    @Override
    public Object getItem(int position) {
        // TODO Auto-generated method stub
        return null;
    }

    @Override
    public long getItemId(int position) {
        // TODO Auto-generated method stub
        return 0;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        // 自定义视图
        listItemView = new ListItemView();
        // 获取list_item布局文件的视图
        if (convertView == null) {
            convertView = listContainer.inflate(R.layout.qa_item, null);
            // 获取控件对象
            listItemView.question = (TextView) convertView.findViewById(R.id.question);
            listItemView.Time = (TextView) convertView
                    .findViewById(R.id.Time);
            listItemView.answerNum = (TextView) convertView
                    .findViewById(R.id.answerNum);
            listItemView.answerHead = (ImageView) convertView
                    .findViewById(R.id.answerHead);
            listItemView.answerName = (TextView) convertView
                    .findViewById(R.id.answerName);
            listItemView.answer = (TextView) convertView
                    .findViewById(R.id.answer);
            listItemView.answerView = convertView
                    .findViewById(R.id.answerView);

            // 设置控件集到convertView
            convertView.setTag(listItemView);
        } else {
            listItemView = (ListItemView) convertView.getTag();
        }

        listItemView.question.setText(QAList.get(position).getQuestion());
        listItemView.Time.setText(QAList.get(position).getTime());

        if(QAList.get(position).getAnswerNum() == 0){
            listItemView.answerNum.setText("暂无答案");
            listItemView.answerView.setVisibility(View.GONE);
        }else{
            listItemView.answerNum.setText("已有：" + QAList.get(position).getAnswerNum()+ "个回答");

            if((QAList.get(position).getAnswer().equals(""))){
                listItemView.answerView.setVisibility(View.GONE);
            }else{
                listItemView.answerView.setVisibility(View.VISIBLE);

                ImgShow.display(listItemView.answerHead, QAList.get(position).getAnswerUserHead());

                listItemView.answerName.setText(QAList.get(position).getAnswerName());
                listItemView.answer.setText(QAList.get(position).getAnswer());
            }

        }


        return convertView;
    }

}
